package cn.eng.basic.service.impl;

import cn.eng.basic.mapper.BaseMapper;
import cn.eng.basic.query.BaseQuery;
import cn.eng.basic.service.IBaseService;
import cn.eng.basic.util.PageList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    /**
     * 查询所有对象
     * @return
     */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    /**
     * 查询单个对象
     * @param id
     * @return
     */
    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    /**
     * 添加一个对象
     * @param t
     */
    @Override
    public void save(T t) {
        baseMapper.save(t);
    }

    /**
     * 删除一个对象
     * @param id
     */
    @Override
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    /**
     * 更新一个对象
     * @param t
     */
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    /**
     * 分页查询
     * @param query
     * @return
     */

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //查询总数量
        Integer count = baseMapper.queryCount(query);
        if(count==null){
            return new PageList<T>();
        }
        List<T> list = baseMapper.queryData(query);
        return new PageList<T>(count, list);
    }

    @Override
    public void patchDel(Long[] ids) {
        baseMapper.patchDel(ids);
    }
}
